from Katana import NodegraphAPI, DrawingModule, Utils, PyXmlIO as XIO, UniqueName
import logging
import ScriptActions as SA
log = logging.getLogger('AOV_X.Node')

emission = "'%s'"%('C[LO]')
class AOV_XNode(NodegraphAPI.SuperTool):

	def __init__(self):
		self.hideNodegraphGroupControls()
		self.addInputPort('In')
		# self.getParameters().createChildString('Deep_path','')
		# self.getParameters().createChildString('Beauty_path','')
		# self.getParameters().createChildString('Position_path','')
		# self.getParameters().createChildString('Normal_path','')
		# self.getParameters().createChildString('Depth_path','')
		# self.getParameters().createChildString('ambient_occlution_path','')
		# self.getParameters().createChildString('cn_path','')
		# self.getParameters().createChildString('aov_merge_path','')
		self.addOutputPort('out')
		self.__buildDefaultNetwork()

	def __buildDefaultNetwork(self):

		AOV_inputpot = self.getSendPort('In')
		AOV_outputpot = self.getReturnPort('out')
		#Create AOCD&ROD node...
		#beauty
		# beauty_AOCD_node = NodegraphAPI.CreateNode('ArnoldOutputChannelDefine',self)
		# beauty_AOCD_node.setBypassed(True)
		# beauty_AOCD_node.setName('Beauty_AOCD')
		# beauty_rod_node = NodegraphAPI.CreateNode('RenderOutputDefine', self)
		# beauty_rod_node.setBypassed(True)
		# beauty_rod_node.setName('Beauty_ROD')
		# beauty_rod_inputport = beauty_rod_node.getInputPortByIndex(0)
		# beauty_rod_inputport.connect(AOV_inputpot)
		# SA.connectAOVnode(beauty_AOCD_node,beauty_rod_node)
		# SA.AOCD_setparameter(beauty_AOCD_node, 'primary', 'RGBA', 'RGBA')
		# SA.ROD_setparameter(beauty_rod_node, 'RGBA', 'color', 'RG')

		# beauty_rod_node.getParameters().getChild('args.renderSettings.outputs.outputName.rendererSettings.channel.value').setValue('Beauty',0)


		#position
		p_AOCD_node = NodegraphAPI.CreateNode('ArnoldOutputChannelDefine', self)
		p_AOCD_node.setBypassed(True)
		p_AOCD_node.setName('position_AOCD')
		p_rod_node = NodegraphAPI.CreateNode('RenderOutputDefine',self)
		p_rod_node.setBypassed(True)
		p_rod_node.setName('position_ROD')
		p_AOCD_inputport = p_AOCD_node.getInputPortByIndex(0)
		p_AOCD_inputport.connect(AOV_inputpot)
		p_AOCD_outputport = p_AOCD_node.getOutputPortByIndex(0)
		p_rod_inputport = p_rod_node.getInputPortByIndex(0)
		SA.AOCD_setparameter(p_AOCD_node, 'Point', 'P', 'VECTOR')
		SA.ROD_setparameter(p_rod_node, 'Point','color', 'Point')
		#AOV_inputpot.connect(p_AOCD_inputport)
		SA.connectAOVnode(p_AOCD_node,p_rod_node)
		p_attr = NodegraphAPI.CreateNode('AttributeSet', self)
		p_attr.setName('position_attr')
		p_attr.setBypassed(True)
		p_attr.getParameter('attributeName').setValue('arnoldGlobalStatements.outputChannels.Point.driverParameters.half_precision',0)
		p_attr.getParameter('attributeType').setValue('float',0)
		p_attr.getParameter('numberValue.i0').setValue(1.0,0)
		p_attr.getParameter('paths.i0').setValue('/root',0)
		p_attr_t = NodegraphAPI.CreateNode('AttributeSet', self)
		p_attr_t.setName('position_exrOptimize')
		p_attr_t.setBypassed(True)
		p_attr_t.getParameter('paths.i0').setValue('/root',0)
		p_attr_t.getParameter('attributeName').setValue('renderSettings.outputs.Point.rendererSettings.convertSettings.exrOptimize',0)
		p_attr_t.getParameter('attributeType').setValue('string',0)
		p_attr_t.getParameter('stringValue.i0').setValue('No',0)
		SA.connectAOVnode(p_rod_node, p_attr_t)
		SA.connectAOVnode(p_attr_t, p_attr)

		#normal
		n_AOCD_node = NodegraphAPI.CreateNode('ArnoldOutputChannelDefine', self)
		n_AOCD_node.setBypassed(True)
		n_AOCD_node.setName('normal_AOCD')
		n_rod_node = NodegraphAPI.CreateNode('RenderOutputDefine',self)
		n_rod_node.setBypassed(True)
		n_rod_node.setName('normal_ROD')
		SA.AOCD_setparameter(n_AOCD_node, 'Normal', 'N', 'VECTOR')
		SA.ROD_setparameter(n_rod_node, 'Normal','color', 'Normal')
		SA.connectAOVnode(n_AOCD_node,n_rod_node)
		n_attr = NodegraphAPI.CreateNode('AttributeSet', self)
		n_attr.setName('normal_attr')
		n_attr.setBypassed(True)
		n_attr.getParameter('attributeName').setValue('arnoldGlobalStatements.outputChannels.Normal.driverParameters.half_precision',0)
		n_attr.getParameter('attributeType').setValue('float',0)
		n_attr.getParameter('numberValue.i0').setValue(1.0,0)
		n_attr.getParameter('paths.i0').setValue('/root',0)
		n_attr_t = NodegraphAPI.CreateNode('AttributeSet', self)
		n_attr_t.setName('normal_exrOptimize')
		n_attr_t.setBypassed(True)
		n_attr_t.getParameter('paths.i0').setValue('/root',0)
		n_attr_t.getParameter('attributeName').setValue('renderSettings.outputs.Normal.rendererSettings.convertSettings.exrOptimize',0)
		n_attr_t.getParameter('attributeType').setValue('string',0)
		n_attr_t.getParameter('stringValue.i0').setValue('No',0)
		SA.connectAOVnode(n_rod_node, n_attr_t)
		SA.connectAOVnode(n_attr_t, n_attr)

		#depth
		z_AOCD_node = NodegraphAPI.CreateNode('ArnoldOutputChannelDefine', self)
		z_AOCD_node.setBypassed(True)
		z_AOCD_node.setName('depth_AOCD')
		z_rod_node = NodegraphAPI.CreateNode('RenderOutputDefine',self)
		z_rod_node.setBypassed(True)
		z_rod_node.setName('depth_ROD')
		SA.AOCD_setparameter(z_AOCD_node, 'Depth', 'Z', 'FLOAT')
		SA.ROD_setparameter(z_rod_node, 'Depth', 'color', 'Depth')
		SA.connectAOVnode(z_AOCD_node,z_rod_node)
		z_attr = NodegraphAPI.CreateNode('AttributeSet', self)
		z_attr.setName('depth_attr')
		z_attr.setBypassed(True)
		z_attr.getParameter('attributeName').setValue('arnoldGlobalStatements.outputChannels.Depth.driverParameters.half_precision',0)
		z_attr.getParameter('attributeType').setValue('float',0)
		z_attr.getParameter('numberValue.i0').setValue(1.0,0)
		z_attr.getParameter('paths.i0').setValue('/root',0)
		z_attr_t = NodegraphAPI.CreateNode('AttributeSet', self)
		z_attr_t.setName('depth_exrOptimize')
		z_attr_t.setBypassed(True)
		z_attr_t.getParameter('paths.i0').setValue('/root',0)
		z_attr_t.getParameter('attributeName').setValue('renderSettings.outputs.Depth.rendererSettings.convertSettings.exrOptimize',0)
		z_attr_t.getParameter('attributeType').setValue('string',0)
		z_attr_t.getParameter('stringValue.i0').setValue('No',0)
		SA.connectAOVnode(z_rod_node, z_attr_t)
		SA.connectAOVnode(z_attr_t, z_attr)

		#motion vector
		mv_AOCD_node = NodegraphAPI.CreateNode('ArnoldOutputChannelDefine', self)
		mv_AOCD_node.setBypassed(True)
		mv_AOCD_node.setName('motionvector_AOCD')
		mv_rod_node = NodegraphAPI.CreateNode('RenderOutputDefine',self)
		mv_rod_node.setBypassed(True)
		mv_rod_node.setName('motionvector_ROD')
		SA.AOCD_setparameter(mv_AOCD_node, 'motion_vector', 'motion_vector', 'RGBA')
		SA.ROD_setparameter(mv_rod_node, 'motion_vector', 'color', 'motion_vector')
		SA.connectAOVnode(mv_AOCD_node,mv_rod_node)
		mv_attr = NodegraphAPI.CreateNode('AttributeSet', self)
		mv_attr.setName('motionvector_attr')
		mv_attr.setBypassed(True)
		mv_attr.getParameter('attributeName').setValue('arnoldGlobalStatements.outputChannels.motion_vector.driverParameters.half_precision',0)
		mv_attr.getParameter('attributeType').setValue('float',0)
		mv_attr.getParameter('numberValue.i0').setValue(1.0,0)
		mv_attr.getParameter('paths.i0').setValue('/root',0)
		mv_attr_t = NodegraphAPI.CreateNode('AttributeSet', self)
		mv_attr_t.setName('motionvector_exrOptimize')
		mv_attr_t.setBypassed(True)
		mv_attr_t.getParameter('paths.i0').setValue('/root',0)
		mv_attr_t.getParameter('attributeName').setValue('renderSettings.outputs.motion_vector.rendererSettings.convertSettings.exrOptimize',0)
		mv_attr_t.getParameter('attributeType').setValue('string',0)
		mv_attr_t.getParameter('stringValue.i0').setValue('No',0)
		SA.connectAOVnode(mv_rod_node, mv_attr_t)
		SA.connectAOVnode(mv_attr_t, mv_attr)

		#ambient occlution
		ao_AOCD_node = NodegraphAPI.CreateNode('ArnoldOutputChannelDefine', self)
		ao_AOCD_node.setBypassed(True)
		ao_AOCD_node.setName('ambientocclution_AOCD')
		ao_rod_node = NodegraphAPI.CreateNode('RenderOutputDefine',self)
		ao_rod_node.setBypassed(True)
		ao_rod_node.setName('ambientocclution_ROD')
		SA.connectAOVnode(ao_AOCD_node,ao_rod_node)
		SA.AOCD_setparameter(ao_AOCD_node, 'ambient_occlution', 'ambient_occlution','RGB')
		SA.ROD_setparameter(ao_rod_node, 'ambient_occlution', 'color', 'ambient_occlution')
		ao_attr = NodegraphAPI.CreateNode('AttributeSet', self)
		ao_attr.setName('ambientocclution_attr')
		ao_attr.setBypassed(True)
		ao_attr.getParameter('attributeName').setValue('arnoldGlobalStatements.outputChannels.ambient_occlution.driverParameters.half_precision',0)
		ao_attr.getParameter('attributeType').setValue('float',0)
		ao_attr.getParameter('numberValue.i0').setValue(1.0,0)
		ao_attr.getParameter('paths.i0').setValue('/root',0)
		ao_attr_t = NodegraphAPI.CreateNode('AttributeSet', self)
		ao_attr_t.setName('ambientocclution_exrOptimize')
		ao_attr_t.setBypassed(True)
		ao_attr_t.getParameter('paths.i0').setValue('/root',0)
		ao_attr_t.getParameter('attributeName').setValue('renderSettings.outputs.ambient_occlution.rendererSettings.convertSettings.exrOptimize',0)
		ao_attr_t.getParameter('attributeType').setValue('string',0)
		ao_attr_t.getParameter('stringValue.i0').setValue('No',0)
		SA.connectAOVnode(ao_rod_node, ao_attr_t)
		SA.connectAOVnode(ao_attr_t, ao_attr)

		#cn
		cn_AOCD_node = NodegraphAPI.CreateNode('ArnoldOutputChannelDefine',self)
		cn_AOCD_node.setBypassed(True)
		cn_AOCD_node.setName('Fresnel_AOCD')
		cn_rod_node = NodegraphAPI.CreateNode('RenderOutputDefine', self)
		cn_rod_node.setBypassed(True)
		cn_rod_node.setName('Fresnel_ROD')
		SA.connectAOVnode(cn_AOCD_node,cn_rod_node)
		SA.AOCD_setparameter(cn_AOCD_node, 'Fresnel', 'Fresnel', 'RGB')
		SA.ROD_setparameter(cn_rod_node, 'Fresnel', 'color', 'Fresnel')
		cn_attr = NodegraphAPI.CreateNode('AttributeSet', self)
		cn_attr.setName('Fresnel_attr')
		cn_attr.setBypassed(True)
		cn_attr.getParameter('attributeName').setValue('arnoldGlobalStatements.outputChannels.Fresnel.driverParameters.half_precision',0)
		cn_attr.getParameter('attributeType').setValue('float',0)
		cn_attr.getParameter('numberValue.i0').setValue(1.0,0)
		cn_attr.getParameter('paths.i0').setValue('/root',0)
		cn_attr_t = NodegraphAPI.CreateNode('AttributeSet', self)
		cn_attr_t.setName('Fresnel_exrOptimize')
		cn_attr_t.setBypassed(True)
		cn_attr_t.getParameter('paths.i0').setValue('/root',0)
		cn_attr_t.getParameter('attributeName').setValue('renderSettings.outputs.Fresnel.rendererSettings.convertSettings.exrOptimize',0)
		cn_attr_t.getParameter('attributeType').setValue('string',0)
		cn_attr_t.getParameter('stringValue.i0').setValue('No',0)
		SA.connectAOVnode(cn_rod_node, cn_attr_t)
		SA.connectAOVnode(cn_attr_t, cn_attr)

		#crypto_object
		co_AOCD_node = NodegraphAPI.CreateNode('ArnoldOutputChannelDefine', self)
		co_AOCD_node.setBypassed(True)
		co_AOCD_node.setName('cryptoobject_AOCD')
		co_rod_node = NodegraphAPI.CreateNode('RenderOutputDefine', self)
		co_rod_node.setBypassed(True)
		co_rod_node.setName('cryptoobject_ROD')
		SA.AOCD_setparameter(co_AOCD_node, 'crypto_object', 'crypto_object', 'RGB')
		SA.ROD_setparameter(co_rod_node, 'crypto_object','color', 'crypto_object')
		SA.connectAOVnode(co_AOCD_node,co_rod_node)
		co_attr = NodegraphAPI.CreateNode('AttributeSet', self)
		co_attr.setName('cryptoobject_attr')
		co_attr.setBypassed(True)
		co_attr.getParameter('attributeName').setValue('arnoldGlobalStatements.outputChannels.crypto_object.driverParameters.half_precision',0)
		co_attr.getParameter('attributeType').setValue('float',0)
		co_attr.getParameter('numberValue.i0').setValue(1.0,0)
		co_attr.getParameter('paths.i0').setValue('/root',0)
		co_attr_t = NodegraphAPI.CreateNode('AttributeSet', self)
		co_attr_t.setName('cryptoobject_exrOptimize')
		co_attr_t.setBypassed(True)
		co_attr_t.getParameter('paths.i0').setValue('/root',0)
		co_attr_t.getParameter('attributeName').setValue('renderSettings.outputs.crypto_object.rendererSettings.convertSettings.exrOptimize',0)
		co_attr_t.getParameter('attributeType').setValue('string',0)
		co_attr_t.getParameter('stringValue.i0').setValue('No',0)
		SA.connectAOVnode(co_rod_node, co_attr_t)
		SA.connectAOVnode(co_attr_t, co_attr)
		#crypto_material
		cm_AOCD_node = NodegraphAPI.CreateNode('ArnoldOutputChannelDefine', self)
		cm_AOCD_node.setBypassed(True)
		cm_AOCD_node.setName('cryptomaterial_AOCD')
		cm_rod_node = NodegraphAPI.CreateNode('RenderOutputDefine', self)
		cm_rod_node.setBypassed(True)
		cm_rod_node.setName('cryptomaterial_ROD')
		cm_output_pot = cm_rod_node.getOutputPortByIndex(0)
		SA.AOCD_setparameter(cm_AOCD_node, 'crypto_material', 'crypto_material', 'RGB')
		SA.ROD_setparameter(cm_rod_node, 'crypto_material','color', 'crypto_material')
		# cm_output_pot.connect(AOV_outputpot)
		SA.connectAOVnode(cm_AOCD_node,cm_rod_node)
		cm_attr = NodegraphAPI.CreateNode('AttributeSet', self)
		cm_attr.setName('cryptomaterial_attr')
		cm_attr.setBypassed(True)
		cm_attr.getParameter('attributeName').setValue('arnoldGlobalStatements.outputChannels.crypto_material.driverParameters.half_precision',0)
		cm_attr.getParameter('attributeType').setValue('float',0)
		cm_attr.getParameter('numberValue.i0').setValue(1.0,0)
		cm_attr.getParameter('paths.i0').setValue('/root',0)
		cm_attr_t = NodegraphAPI.CreateNode('AttributeSet', self)
		cm_attr_t.setName('cryptomaterial_exrOptimize')
		cm_attr_t.setBypassed(True)
		cm_attr_t.getParameter('paths.i0').setValue('/root',0)
		cm_attr_t.getParameter('attributeName').setValue('renderSettings.outputs.crypto_material.rendererSettings.convertSettings.exrOptimize',0)
		cm_attr_t.getParameter('attributeType').setValue('string',0)
		cm_attr_t.getParameter('stringValue.i0').setValue('No',0)
		# cm_attr.getOutputPortByIndex(0).connect(AOV_outputpot)
		SA.connectAOVnode(cm_rod_node, cm_attr_t)
		SA.connectAOVnode(cm_attr_t, cm_attr)

		deep_aocd_node = NodegraphAPI.CreateNode('ArnoldOutputChannelDefine', self)
		deep_aocd_node.setBypassed(True)
		deep_aocd_node.setName('deep_aocd')
		deep_aocd_node.getParameter('driver').setValue('driver_deepexr',0)
		deep_aocd_node.getParameter('name').setValue('Deep',0)
		# deep_rod_node = NodegraphAPI.CreateNode('RenderOutputDefine', self)
		# deep_rod_node.setBypassed(True)
		# deep_rod_node.setName('deep_rod')
		# SA.ROD_setparameter(deep_rod_node, 'deep','deep', 'Deep')
		# deep_rod_node.getOutputPortByIndex(0).connect(AOV_outputpot)
		# deep_aocd_node.getOutputPortByIndex(0).connect(AOV_outputpot)


		emission_AOCD_node = NodegraphAPI.CreateNode('ArnoldOutputChannelDefine', self)
		emission_AOCD_node.setBypassed(True)
		emission_AOCD_node.setName('emission_AOCD')
		emission_rod_node = NodegraphAPI.CreateNode('RenderOutputDefine', self)
		emission_rod_node.setBypassed(True)
		emission_rod_node.setName('emission_ROD')
		# cm_output_pot.connect(AOV_outputpot)
		SA.connectAOVnode(emission_AOCD_node,emission_rod_node)
		emission_attr = NodegraphAPI.CreateNode('AttributeSet', self)
		emission_attr.setName('emission_attr')
		emission_attr.setBypassed(True)
		emission_attr.getParameter('attributeName').setValue('arnoldGlobalStatements.outputChannels.Emission.driverParameters.half_precision',0)
		emission_attr.getParameter('attributeType').setValue('float',0)
		emission_attr.getParameter('numberValue.i0').setValue(1.0,0)
		emission_attr.getParameter('paths.i0').setValue('/root',0)
		emission_attr_t = NodegraphAPI.CreateNode('AttributeSet', self)
		emission_attr_t.setName('emission_exrOptimize')
		emission_attr_t.setBypassed(True)
		emission_attr_t.getParameter('paths.i0').setValue('/root',0)
		emission_attr_t.getParameter('attributeName').setValue('renderSettings.outputs.Emission.rendererSettings.convertSettings.exrOptimize',0)
		emission_attr_t.getParameter('attributeType').setValue('string',0)
		emission_attr_t.getParameter('stringValue.i0').setValue('No',0)
		SA.connectAOVnode(emission_AOCD_node, emission_rod_node)
		SA.connectAOVnode(emission_rod_node, emission_attr)
		SA.connectAOVnode(emission_attr, emission_attr_t)
		emission_attr_t.getOutputPortByIndex(0).connect(AOV_outputpot)
		SA.set_aocd_parameter(emission_AOCD_node, "'Emission'", "'Emission'", emission)
		SA.set_rod_parameter(emission_rod_node, "'Emission'", "'Emission'")


		# deep_attr = NodegraphAPI.CreateNode('AttributeSet', self)
		# deep_attr.setBypassed(True)
		# deep_attr.setName('deep_attr')
		# deep_attr.getParameter('attributeName').setValue('arnoldGlobalStatements.outputChannels.Deep.driverParameters.subpixel_merge',0)
		# deep_attr.getParameter('attributeType').setValue('integer',0)
		# deep_attr.getParameter('paths.i0').setValue('/root',0)
		# deep_attr.getOutputPortByIndex(0).connect(AOV_outputpot)
		# with NodegraphAPI.InteractiveContext():
		# 	deep_rod_node.getParameter('args.renderSettings.outputs.outputName.locationType.enable').setValue(1, 0)
		# 	deep_rod_node.getParameter('args.renderSettings.outputs.outputName.locationType.value').setValue('file',0)
		# 	deep_rod_node._flushAll()
		# 	deep_rod_node.checkDynamicParameters()
		# 	deep_rod_node.getParameter('args.renderSettings.outputs.outputName.locationSettings.renderLocation.enable').setValue(1,0)
		# 	deep_rod_node.getParameter('args.renderSettings.outputs.outputName.locationSettings.renderLocation.value').setExpression('self.getParent().Deep_path' + "+" + "self.getNodeName().split('_')[0]" + "+" + "'"+'_deep/'  + "'" + '+'  + "project.assetID.split('.')[0].split('/')[-1]"+"+"+"'"+'.####.exr' + "'",0)
		# 	deep_rod_node.getParameter('args.renderSettings.outputs.outputName.locationSettings.renderLocation.value').setExpressionFlag(True)
		# SA.connectAOVnode(deep_aocd_node, deep_rod_node)
		# SA.connectAOVnode(deep_rod_node, deep_attr)

		#mergeexr
		# merge_rod_node = NodegraphAPI.CreateNode('RenderOutputDefine', self)
		# merge_rod_node.setBypassed(False)
		# merge_rod_node.setName('Merge_ROD')
		# merge_rod_input = merge_rod_node.getInputPortByIndex(0)
		# merge_rod_output = merge_rod_node.getOutputPortByIndex(0)
		# merge_rod_output.connect(AOV_outputpot)
		# SA.set_mergenode_parameter(merge_rod_node, 'self.getParent().aov_merge_path')

		SA.connectAOVnode(p_attr,n_AOCD_node)
		SA.connectAOVnode(n_attr,z_AOCD_node)
		SA.connectAOVnode(z_attr,mv_AOCD_node)
		SA.connectAOVnode(mv_attr,ao_AOCD_node)
		SA.connectAOVnode(ao_attr,cn_AOCD_node)
		SA.connectAOVnode(cn_attr,co_AOCD_node)
		SA.connectAOVnode(co_attr,cm_AOCD_node)
		SA.connectAOVnode(cm_attr,deep_aocd_node)
		SA.connectAOVnode(deep_aocd_node, emission_AOCD_node)

		# SA.connectAOVnode(co_rod_node,cm_AOCD_node)



	def addParameterHints(self, attrName, inputDict):
		inputDict.update(_ExtraHints.get(attrName, {}))




_ExtraHints = {
	'AOV_X.Deep_path':{
	'widget':'assetIdOutput',
	'dirsOnly':'True',
	},

}
